﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;
using Acms.AcmsControls;

public partial class Templates_Office_Display : BaseTemplateUserControl
{
    protected override void OnInit(EventArgs e)
    {
        acmsAddress.DataBound += new EventHandler(acmsAddress_DataBound);
        base.OnInit(e);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    void acmsAddress_DataBound(object sender, EventArgs e)
    {
        if (acmsAddress.Text != null && acmsAddress.Text.Trim().EndsWith(","))
        {
            acmsAddress.Text = acmsAddress.Text.Trim().Remove(acmsAddress.Text.Trim().Length - 1);
        }
    }

    protected override void DataBindChildren()
    {
        PostControl addressControl = AcmsContext.CurrentPost.FindPostControl("Address");
        if (addressControl != null)
        {
            TextContentControlProperties prop = addressControl.GetProperties<TextContentControlProperties>();
            if (prop != null && !string.IsNullOrEmpty(prop.Text))
            {
                map.Address = prop.Text;
            }
        }

        PostControl townControl = AcmsContext.CurrentPost.FindPostControl("Town");
        if (townControl != null)
        {
            TextContentControlProperties prop = townControl.GetProperties<TextContentControlProperties>();
            if (prop != null && !string.IsNullOrEmpty(prop.Text))
            {
                map.Address = string.Concat(map.Address, ", ", prop.Text).Trim();
            }
        }

        PostControl postCodeControl = AcmsContext.CurrentPost.FindPostControl("PostCode");
        if (postCodeControl != null)
        {
            TextContentControlProperties prop = postCodeControl.GetProperties<TextContentControlProperties>();
            if (prop != null && !string.IsNullOrEmpty(prop.Text))
            {
                map.Address = string.Concat(map.Address, " ", prop.Text).Trim();
            }
        }

        PostControl emailControl = AcmsContext.CurrentPost.FindPostControl("Email");
        if (emailControl != null)
        {
            TextContentControlProperties prop = emailControl.GetProperties<TextContentControlProperties>();
            if (prop != null && !string.IsNullOrEmpty(prop.Text))
            {
                lnkEmail.NavigateUrl = string.Concat("mailto:", prop.Text.Trim());
                lnkEmail.Text = prop.Text.Trim();
            }
        }
        PostControl latatudeControl = AcmsContext.CurrentPost.FindPostControl("Latitude");
        if (latatudeControl != null)
        {
            TextContentControlProperties prop = latatudeControl.GetProperties<TextContentControlProperties>();
            if (prop != null && !string.IsNullOrEmpty(prop.Text))
            {
                try
                {
                    map.Latitude = float.Parse(prop.Text.Trim());
                }
                catch
                {
                }
            }
        }
        PostControl longitudeControl = AcmsContext.CurrentPost.FindPostControl("Longitude");
        if (longitudeControl != null)
        {
            TextContentControlProperties prop = longitudeControl.GetProperties<TextContentControlProperties>();
            if (prop != null && !string.IsNullOrEmpty(prop.Text))
            {
                try
                {
                    map.Longitude = float.Parse(prop.Text.Trim());
                }
                catch
                {
                }
            }
        }
        base.DataBindChildren();
    }
}
